package com.ctp.ui.table.renderer;

import java.awt.Component;
import java.math.BigDecimal;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

public class TableNumericRenderer implements TableCellRenderer {

	private JLabel label = new JLabel();

	private int digitCnt = 0;

	public TableNumericRenderer(int digitCnt) {
		label.setFont(UIManager.getFont("Table.font"));
		label.setHorizontalAlignment(JLabel.RIGHT);

		this.digitCnt = digitCnt;
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		String text = "-";
		if (value != null) {
			try {
				BigDecimal decimal = new BigDecimal(value.toString());
				decimal = decimal.setScale(digitCnt, BigDecimal.ROUND_HALF_UP);

				text = decimal.toString();
			} catch (Exception e) {
			}
		}

		label.setText(text);
		return label;
	}

}
